<?php

/**
 * Description of Users
 *
 * @author Shang
 */
class zShang_Model_DbTable_Users extends Zend_Db_Table_Abstract {

    const TABLE_NAME = 'users';

    protected $_name = self::TABLE_NAME;
    protected $_rowClass = 'zShang_Model_User';

    public function getUsers() {
        $select = $this->select()
                ->setIntegrityCheck(FALSE);
        $users = $this->fetchAll();
        return $users;
    }

    public function getUser($id) {
        return $this->fetchRow("id = {$id}");
    }

    public function insertUser($user) {
        if (!$this->hasUser($user['id'])) {
            $data = array(
                'username' => $user['username'],
                'password' => md5($user['password']),
                'role' => $user['role']
            );
            return $this->insert($data);
        }
    }

    public function hasUser($id) {
        $user = $this->getById($id);
        $flag = FALSE;
        if (!empty($user)) {
            $flag = TRUE;
        }
        return $flag;
    }

}